var maxProfit = function (prices) {
  let n = prices.length;
  let profit_1_in = -prices[0];
  let profit_1_out = 0;
  for (let i = 1; i < n; i++) {
    profit_2_out = Math.max(profit_2_out, profit_2_in + prices[i]);
    // 第二次买入后的利润，第一次卖出时的利润 - prices[i]
    profit_2_in = Math.max(profit_2_in, profit_1_out - prices[i]);
    profit_1_out = Math.max(profit_1_out, profit_1_in + prices[i]);
    // 第一次买入时，利润为 -prices[i]
    profit_1_in = Math.max(profit_1_in, -prices[i]);
  }
  return profit_2_out;
}